package com.imstlife.vending.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.imstlife.vending.R;
import com.imstlife.vending.utils.DrinkCountUtil;
import com.imstlife.vending.utils.GlideUtil;
import com.imstlife.vending.utils.SharePersistentUtil;
import com.imstlife.vending.vo.DrinkInfo;

import java.util.List;

/**
 * Created by lihaifeng on 17/2/21.
 */

public class ManageAdapter extends BaseAdapter {

    List<DrinkInfo> drinkInfos;

    Context context;

    public ManageAdapter(List<DrinkInfo> drinkInfos , Context context) {
        this.drinkInfos=drinkInfos;
        this.context = context;

    }

    @Override
    public int getCount() {
        return drinkInfos.size();
    }

    @Override
    public Object getItem(int position) {
        return drinkInfos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if(convertView ==null){
            convertView = LayoutInflater.from(context).inflate(R.layout.item_manage_drink,null);
            holder = new ViewHolder();
            holder.ivDrinkPic = (ImageView) convertView.findViewById(R.id.iv_item_manage_drink_pic);
            holder.tvName = (TextView) convertView.findViewById(R.id.tv_item_manage_name);
            holder.tvCount = (TextView) convertView.findViewById(R.id.tv_item_manage_count);
            holder.btnJia = (Button) convertView.findViewById(R.id.btn_item_manage_drink_jia);
            holder.btnJian = (Button) convertView.findViewById(R.id.btn_item_manage_drink_jian);
            convertView.setTag(holder);

        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        final int count = DrinkCountUtil.getDrinkCountByNum(context,drinkInfos.get(position).getNum()+"");
        holder.tvName.setText("货道"+drinkInfos.get(position).getNum()+" : "+drinkInfos.get(position).getName());
        holder.tvCount.setText(count+"");
        holder.btnJia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 int count = DrinkCountUtil.getDrinkCountByNum(context,drinkInfos.get(position).getNum()+"");
                if(count<5){
                    holder.tvCount.setText(DrinkCountUtil.updateCount(context,drinkInfos.get(position).getNum()+"",1,1)+"");
                }

            }
        });
        holder.btnJian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 int count = DrinkCountUtil.getDrinkCountByNum(context,drinkInfos.get(position).getNum()+"");
                if(count>0){
                    holder.tvCount.setText(DrinkCountUtil.updateCount(context,drinkInfos.get(position).getNum()+"",2,1)+"");
                }

            }
        });
        GlideUtil.dontAnimLoad(context,drinkInfos.get(position).getMinPicUrl(),holder.ivDrinkPic);

        return convertView;
    }
    class ViewHolder{

        ImageView ivDrinkPic;
        TextView tvName;
        TextView tvCount;
        Button btnJia;
        Button btnJian;
    }
}
